package cn.edu.paisi.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.paisi.annotation.Auth;
import cn.edu.paisi.entity.Admin;
import cn.edu.paisi.entity.Students;
import cn.edu.paisi.entity.Teachers;
import cn.edu.paisi.entity.User;
import cn.edu.paisi.service.IAdminService;
import cn.edu.paisi.service.IStudentsService;
import cn.edu.paisi.service.ITeachersService;
import cn.edu.paisi.util.JwtUtil;
import cn.edu.paisi.util.RestResult;

@RestController
@CrossOrigin
public class UserController {
	@Autowired
	private JwtUtil jwtUtil;
	@Autowired
	private ITeachersService teaService;
	@Autowired
	private IStudentsService stuService;
	@Autowired
	private IAdminService adminService;
	private Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@RequestMapping("/loginTea")
	public RestResult<Teachers> login(Teachers tea) {
		logger.info("教师用户登录：" + tea.getTeaId());
		Teachers teachers = teaService.getById(tea.getTeaId());
		if(teachers!=null && teachers.getTeaPsw().equals(tea.getTeaPsw())) {
			String jwt = jwtUtil.createJwtTea(teachers); //生成token
			return RestResult.ok(teachers, jwt);
		}else {
			return RestResult.failed("教师编号或者密码错误");
		}
	}
	
	@RequestMapping("/loginStu") 
	public RestResult<Students> login(Students stu){
		logger.info("学生用户登录：" + stu.getStuId());
		Students students = stuService.getById(stu.getStuId());
		if(students!=null&&students.getStuPsw().equals(stu.getStuPsw())) {
			String jwtStu = jwtUtil.createJwtStu(students);
			return RestResult.ok(students,jwtStu);
		}else {
			return RestResult.failed("学生编号或者密码错误");
		}
	}
	
	@RequestMapping("/loginAdmin")
	public RestResult<Admin> login(Admin admin){
		logger.info("管理员登录：" + admin.getAdmAccount());
		Admin adm = adminService.getById(admin.getAdmAccount());
		if(adm!=null&&adm.getAdmPwd().equals(adm.getAdmPwd())) {
			String jwtAdmin = jwtUtil.createJwtAdmin(admin);
			return RestResult.ok(adm,jwtAdmin);
		}else {
			return RestResult.failed("管理员账号或者密码错误");
		}
	}
	
	@RequestMapping("/getTea")
	public RestResult<Teachers> getTea() {
		List<Teachers> list = teaService.list();
		return  RestResult.ok(list);
	}
	
	@RequestMapping("/getStu")
	public RestResult<Students> getStu(){
		 List<Students> list = stuService.list();
		 return RestResult.ok(list);
	}
	
	
	
	@RequestMapping("/addTea")
	public RestResult<Teachers> addTea(Teachers tea) {
		 teaService.save(tea);
		 return new RestResult<>();
	}
	
	@RequestMapping("/addStu")
	public RestResult<Students> addStu(Students stu) {
		stuService.save(stu);
		return new RestResult<Students>();
	}
	
	
	@RequestMapping("/updateTea")
	public RestResult<Teachers> update(Teachers tea) {
	    teaService.updateById(tea);
		return new RestResult<Teachers>();
	}
	
	@RequestMapping("/updateStu")
	public RestResult<Students> update(Students stu) {
		stuService.updateById(stu);
		return new RestResult<>();
	}
	
	@RequestMapping("/deleteTea")
	public RestResult<Teachers> deleteTea(Integer teaIds) {
		teaService.removeByIds(Arrays.asList(teaIds));
		return new RestResult<>();
	}
	
	@RequestMapping("/deleteStu")
	public RestResult<Students> deleteStu(Integer stuIds) {
		stuService.removeByIds(Arrays.asList(stuIds));
		return new RestResult<>();
	}
	
	@RequestMapping("/getUser")
	@Auth(roles = "TEACHERS")
	public User getUser(Integer userId,HttpServletRequest req) {
		String role = (String)req.getAttribute("role");
		logger.info("当前登录用户角色是：" + role);
		return new User();
	}
	
	
}
